package com.open.party.cloud.view.task

import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.TextView
import androidx.appcompat.widget.AppCompatSpinner
import androidx.core.content.ContextCompat
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppEventBusTitleBaseActivity
import com.open.party.cloud.R
import com.open.party.cloud.model.DictionaryBean
import com.open.party.cloud.model.PartyTaskVo
import com.open.party.cloud.viewModel.TaskViewModel
import com.sinothk.android.utils.XUtils
import com.sinothk.switchTabView.style1.UIUtil
import kotlinx.android.synthetic.main.free_photo_creator.levelSpinner
import kotlinx.android.synthetic.main.task_check_add.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class TaskCheckAddActivity : AppEventBusTitleBaseActivity(), View.OnClickListener {

    private var id = ""
    private var viewModel: TaskViewModel? = null
    override fun getLayoutResId(): Int = R.layout.task_check_add

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        id = intent.getStringExtra("id") as String
        setViewTitle("任务审核")
        viewModel = TaskViewModel()

        initLevelView()

        addCheckBtn.setOnClickListener(this)
    }

    // ===========================
    var typeDicBean: DictionaryBean? = null

    private fun initLevelView() {
        val levelBeanList = ArrayList<DictionaryBean>()
        //  YTH="已退回", YWC="已完成"
        levelBeanList.add(DictionaryBean("YWC", "已完成"))
        levelBeanList.add(DictionaryBean("YTH", "退回"))

        val levelValueList = ArrayList<String>()
        for (levelBean in levelBeanList) {
            levelValueList.add(levelBean.desc)
        }

        initSpinnerView(levelSpinner, levelValueList, object : AdapterView.OnItemSelectedListener {
            override fun onNothingSelected(parent: AdapterView<*>?) {
            }

            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {

                if (view != null) {
                    val tv = view as TextView
                    tv.setTextColor(ContextCompat.getColor(this@TaskCheckAddActivity, R.color.page_content))
                    tv.paint.textSize = UIUtil.sp2px(this@TaskCheckAddActivity, 14f).toFloat()
                    tv.gravity = Gravity.END
                    typeDicBean = levelBeanList[position]
                }
            }
        })
    }

    private var spinnerAdapter: ArrayAdapter<String>? = null

    private fun initSpinnerView(
        spinner: AppCompatSpinner, streetStrList: ArrayList<String>, itemSelectedListener: AdapterView.OnItemSelectedListener?
    ) {
        spinnerAdapter = ArrayAdapter(this, R.layout.street_spinner_item, streetStrList!!)
        spinnerAdapter!!.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.adapter = spinnerAdapter
        spinner.dropDownVerticalOffset = 100

        spinner.onItemSelectedListener = itemSelectedListener
    }

    // ===========================================================

    override fun onClick(v: View?) {
        when (v) {
            addCheckBtn -> {
                submit()
            }
        }
    }

    private fun submit() {
        if (!XUtils.net().isConnected) {
            XUtils.toast().show(R.string.net_error)
            return
        }

        if (typeDicBean == null) {
            XUtils.toast().show("请选择审批结果")
            return
        }

        val describeStr: String = describeEt.text.toString()
        if (XUtils.string().isEmpty(describeStr)) {
            XUtils.toast().show("请输入描述内容")
            return
        }

        val vo = PartyTaskVo()
        vo.id = id
        vo.state = typeDicBean
        vo.backDesc = describeStr

        showLoadingDialog("正在提交")
        viewModel?.submitTaskCheck(vo)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<String>) {
        if (result.eventType != "submitTaskCheck") {
            return
        }

        hideLoadingDialog()

        when (result.code) {
            ResultInfo.SUCCESS -> {
                XUtils.toast().show("提交成功")
                finish()
            }
            ResultInfo.TOKEN_OVERDUE -> {
            }
            else -> {
                XUtils.toast().show(result.msg)
            }
        }
    }


}